package com.online.fund.control;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.online.fund.service.SharesMoniterConfigService;
import com.online.fund.spider.entity.SharesMoniterConfig;
import com.online.fund.spider.entity.SharesRefIdInfo;

/**
 * 
 * <p>Description: </p>
 * @date 2018年2月6日
 * @author 张飞 
 * @version 1.0
 * <p>Company:</p>
 * <p>Copyright:Copyright(c)2017</p>
 */
@RestController
public class SharesMoniterConfigController {
	
	//默认通过的邮箱
	public static final String NOTIFY_EMAIL = "278772487@qq.com";
	
	@Resource
	private SharesMoniterConfigService configService;
	
	@PostMapping("/sharesMoniterConfig/add")
	public ResultMsg addSharesMoniterConfig(@RequestBody SharesMoniterConfig config) {
		if (config.getSharesCode() == null || "".equals(config.getSharesCode())) {
			return new ResultMsg("-1", "股票编码不能为空");
		}
		
		if (config.getMaxValue() == null && config.getMinValue() == null) {
			return new ResultMsg("-1", "股票价格变动值不能为空");
		}
		
		SharesRefIdInfo sharesRefIdInfo = configService.getSharesRefIdInfo(config.getSharesCode());
		if (sharesRefIdInfo == null) {
			return new ResultMsg("-1", "股票编码错误，股票不存在");
		}
		
		config.setSharesName(sharesRefIdInfo.getSharesName());
		config.setRefId(sharesRefIdInfo.getRefId());
		config.setNotifyEmail(NOTIFY_EMAIL);
		configService.addSharesMoniterConfig(config);
		return new ResultMsg("1", "保存数据成功");
	}
	
	@PostMapping("/sharesMoniterConfig/update")
	public ResultMsg updateSharesMoniterConfig(@RequestBody SharesMoniterConfig config) {
		if (config.getId() == null) {
			return new ResultMsg("-1", "数据id不能为空");
		}
		
		if (config.getMaxValue() == null && config.getMinValue() == null) {
			return new ResultMsg("-1", "股票价格变动值不能为空");
		}
		
		configService.updateSharesMoniterConfig(config);
		return new ResultMsg("1", "更新数据成功");
	}
	
	@GetMapping("/sharesMoniterConfig/delete")
	public ResultMsg updateSharesMoniterConfig(Long id) {
		configService.deleteSharesMoniterConfig(id);
		return new ResultMsg("1", "更新数据成功");
	}
	
	@GetMapping("/sharesMoniterConfig/getSharesMoniterConfigById")
	public ResultMsg getSharesMoniterConfigById(Long id) {
		SharesMoniterConfig config = configService.getSharesMoniterConfigById(id);
		if (config == null) {
			return new ResultMsg("-1", "数据不存在");
		}
		
		return new ResultMsg("1", "获取数据成功", config);
	}
	
	@GetMapping("/sharesMoniterConfig/getSharesMoniterConfigList")
	public ResultMsg getSharesMoniterConfigList() {
		List<SharesMoniterConfig> configList = configService.getSharesMoniterConfigList(null);
		
		return new ResultMsg("1", "获取数据成功", configList);
	}

	public static void main(String[] args) {
		
	}
}
